﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lead2012.Utility;

namespace Lead2012.Business
{
    public class CalendarItemRepository
    {
        // Create an instance of entities framework
        Lead2012Entities entities = new Lead2012Entities();

        public List<CalendarItem> GetAll(int userId, DateTime startDate)
        {
            DateTime startTime = LeadTools.StartOfDay(startDate);
            DateTime endTime=LeadTools.EndOfDay(startDate);

            return (from c in entities.CalendarItems
                    where c.UserId == userId && c.StartDate >= startTime && c.StartDate <= endTime
                    select c).ToList();
        }
        public List<CalendarItem> GetAllByLead(int leadId)
        {
            return (from c in entities.CalendarItems
                    where c.LeadId == leadId
                    select c).ToList();
        }
        public List<CalendarItem> GetAll(int userId)
        {
            return (from c in entities.CalendarItems
                    where c.UserId == userId
                    select c).ToList();
        }
        public List<CalendarItem> GetAll()
        {
            return (from c in entities.CalendarItems select c).ToList();
        }


        public CalendarItem GetSingle(int id)
        {
            return (from c in entities.CalendarItems where c.ItemId == id select c).FirstOrDefault();
        }
        public void Create(CalendarItem _object)
        {
            entities.AddToCalendarItems(_object);
            entities.SaveChanges();
        }
        public void Update(CalendarItem _object)
        {
            entities.SaveChanges();
        }
        public void Delete(int id)
        {
            var _object = (from _list in entities.CalendarItems where _list.ItemId == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }
    }
}